local qiaoqu = fk.CreateSkill{
  name = "hanqing__qiaoqu",
}

Fk:loadTranslationTable{
  ["hanqing__qiaoqu"] = "巧取",
  [":hanqing__qiaoqu"] = "出牌阶段限一次，你可以令一名角色弃置一张牌，然后令两名角色拼点：未赢者获得因此弃置的牌，赢者获得未赢者的拼点牌，"..
  "你获得赢者的拼点牌。",

  ["#hanqing__qiaoqu"] = "巧取：令一名角色弃一张牌，然后选两名角色拼点，你和这两名角色分别获得拼点牌和弃置的牌",
  ["#hanqing__qiaoqu-choose"] = "巧取：选择两名角色拼点",
}

qiaoqu:addEffect("active", {
  anim_type = "control",
  card_num = 0,
  target_num = 1,
  prompt = "#hanqing__qiaoqu",
  can_use = function(self, player)
    return player:usedSkillTimes(qiaoqu.name, Player.HistoryPhase) == 0
  end,
  card_filter = Util.FalseFunc,
  target_filter = function(self, player, to_select, selected)
    return #selected == 0 and not to_select:isNude()
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    local target = effect.tos[1]
    local card1 = room:askToDiscard(target, {
      min_num = 1,
      max_num = 1,
      include_equip = true,
      skill_name = qiaoqu.name,
      cancelable = false,
    })
    if #card1 == 0 or player.dead then return end
    if not table.find(room.alive_players, function (p)
      return table.find(room:getOtherPlayers(p, false), function (q)
        return p:canPindian(q)
      end) ~= nil
    end) then return end
    local success, dat = room:askToUseActiveSkill(player, {
      skill_name = "#hanqing__qiaoqu_active",
      prompt = "#hanqing__qiaoqu-choose",
      cancelable = false,
      no_indicate = false,
    })
    if not (success and dat) then
      dat = {}
      dat[1] = table.find(room.alive_players, function (p)
        return table.find(room:getOtherPlayers(p, false), function (q)
          return p:canPindian(q)
        end) ~= nil
      end)
      dat[2] = table.find(room:getOtherPlayers(dat[1], false), function (p)
        return dat[1]:canPindian(p)
      end)
    end
    local targets = dat.targets
    local pindian = targets[1]:pindian({targets[2]}, qiaoqu.name)
    room:delay(1000)
    if pindian.results[targets[2]].winner == targets[1] then
      if not targets[2].dead and table.contains(room.discard_pile, card1[1]) then
        room:obtainCard(dat.targets[2], card1, true, fk.ReasonJustMove, dat.targets[2], qiaoqu.name)
      end
      if not targets[1].dead and pindian.results[targets[2]].toCard and
        room:getCardArea(pindian.results[targets[2]].toCard) == Card.DiscardPile then
        room:obtainCard(dat.targets[1], pindian.results[targets[2]].toCard, true, fk.ReasonJustMove, dat.targets[1], qiaoqu.name)
      end
      if not player.dead and pindian.fromCard and room:getCardArea(pindian.fromCard) == Card.DiscardPile then
        room:obtainCard(player, pindian.fromCard, true, fk.ReasonJustMove, player, qiaoqu.name)
      end
    elseif pindian.results[targets[2]].winner == targets[2] then
      if not targets[1].dead and table.contains(room.discard_pile, card1[1]) then
        room:obtainCard(dat.targets[1], card1, true, fk.ReasonJustMove, dat.targets[1], qiaoqu.name)
      end
      if not targets[2].dead and pindian.fromCard and room:getCardArea(pindian.fromCard) == Card.DiscardPile then
        room:obtainCard(dat.targets[2], pindian.fromCard, true, fk.ReasonJustMove, dat.targets[2], qiaoqu.name)
      end
      if not player.dead and pindian.results[targets[2]].toCard and
        room:getCardArea(pindian.results[targets[2]].toCard) == Card.DiscardPile then
        room:obtainCard(player, pindian.results[targets[2]].toCard, true, fk.ReasonJustMove, player, qiaoqu.name)
      end
    elseif table.contains(room.discard_pile, card1[1]) then
      room:sortByAction(dat.targets)
      for _, p in ipairs(dat.targets) do
        if not p.dead then
          room:obtainCard(p, card1, true, fk.ReasonJustMove, p, qiaoqu.name)
          return
        end
      end
    end
  end,
})

return qiaoqu
